package com.example.hu.UI;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.csht.facelib.manager.CshtCompareUtil;
import com.csht.facelib.utils.FileUtil;
import com.csht.facelib.utils.SpUtils;
import com.csht.facelib.utils.ToastUtils;
import com.example.hu.R;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import androidx.annotation.Nullable;

public class FaceCompareActivity extends BaseActivity {

    private TextView tv_result;
    private ImageView iv_pic1,iv_pic2;
    private ExecutorService es = Executors.newSingleThreadExecutor();
    private Future future;
    private String filePath1,filePath2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_compare);

        init();
    }

    private void init() {
        tv_result = findViewById(R.id.tv_result);
        iv_pic1 = findViewById(R.id.iv_pic1);
        iv_pic2 = findViewById(R.id.iv_pic2);
        findViewById(R.id.btn_camera1).setOnClickListener(this);
        findViewById(R.id.btn_camera2).setOnClickListener(this);
        findViewById(R.id.btn_compare).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_camera1:
                startActivityForResult(new Intent(this,CameraActivity.class),1001);
                break;
            case R.id.btn_camera2:
                startActivityForResult(new Intent(this,CameraActivity.class),1002);
                break;
            case R.id.btn_compare:
                if (TextUtils.isEmpty(filePath1)){
                    ToastUtils.showToast("请选择第一张图片");
                } else if (TextUtils.isEmpty(filePath2)){
                    ToastUtils.showToast("请选择第二张图片");
                } else {
                    comparePhoto();
                }
                break;
        }
    }

    private void comparePhoto() {
        if (future != null && !future.isDone()) {
            ToastUtils.showToast("正在比对中，请稍后再试");
            return;
        }
        future = es.submit(new Runnable() {
            @Override
            public void run() {
                byte[] data1 = CshtCompareUtil.getInstance().getFeatureFromData(FileUtil.file2Bytes(filePath1),true);
                byte[] data2 = CshtCompareUtil.getInstance().getFeatureFromData(FileUtil.file2Bytes(filePath2),false);
                if (data1 == null){
                    ToastUtils.showToast("第一张照片未检测到人脸");
                } else if (data2 == null){
                    ToastUtils.showToast("第二张照片未检测到人脸");
                } else {
                    float[] scores = CshtCompareUtil.getInstance().getNNCompareScore(data1, data2);
                    if (scores != null) {
                        final int score = (int) (scores[0] * 100);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (score > SpUtils.getThreshold()){
                                    tv_result.setText("比对通过：" + score + "分");
                                    tv_result.setTextColor(Color.GREEN);
                                } else {
                                    tv_result.setText("比对未通过：" + score + "分");
                                    tv_result.setTextColor(Color.RED);
                                }
                            }
                        });
                    }
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            if (resultCode == RESULT_OK && data != null){
                if (requestCode == 1001){
                    filePath1 = data.getStringExtra("filePath");
                    iv_pic1.setImageBitmap(BitmapFactory.decodeFile(filePath1));
                } else if (requestCode == 1002){
                    filePath2 = data.getStringExtra("filePath");
                    iv_pic2.setImageBitmap(BitmapFactory.decodeFile(filePath2));
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
